package com.eco.fanliapp.jd;

import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import jd.union.open.goods.bigfield.query.request.BigFieldGoodsReq;
import jd.union.open.goods.bigfield.query.request.UnionOpenGoodsBigfieldQueryRequest;
import jd.union.open.goods.bigfield.query.response.UnionOpenGoodsBigfieldQueryResponse;
import jd.union.open.goods.promotiongoodsinfo.query.request.UnionOpenGoodsPromotiongoodsinfoQueryRequest;
import jd.union.open.goods.promotiongoodsinfo.query.response.UnionOpenGoodsPromotiongoodsinfoQueryResponse;
import jd.union.open.goods.query.request.GoodsReq;
import jd.union.open.goods.query.request.UnionOpenGoodsQueryRequest;
import jd.union.open.goods.query.response.UnionOpenGoodsQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Service
@Slf4j
public class JDSearchController {

    public static void main(String[] args) {
        try {
            JdClient client = new DefaultJdClient(JDConfig.JD_URL, "", JDConfig.JD_APPKEY, JDConfig.JD_APPSECRET);
            UnionOpenGoodsPromotiongoodsinfoQueryRequest request = new UnionOpenGoodsPromotiongoodsinfoQueryRequest();
            request.setSkuIds("48230620926");
            UnionOpenGoodsPromotiongoodsinfoQueryResponse response = client.execute(request);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/jd/item/query", method = RequestMethod.POST)
    public UnionOpenGoodsQueryResponse jdGoodsQuery(String keyword, Long pageNum, Long pageSize, String sort, Boolean hasCoupon) {
        JdClient client = new DefaultJdClient(JDConfig.JD_URL, "", JDConfig.JD_APPKEY, JDConfig.JD_APPSECRET);
        UnionOpenGoodsQueryRequest request = new UnionOpenGoodsQueryRequest();
        GoodsReq goodsReqDTO = new GoodsReq();
        goodsReqDTO.setPageSize(pageSize.intValue());
        goodsReqDTO.setPageIndex(pageNum.intValue());
        goodsReqDTO.setKeyword(keyword);
        if (hasCoupon != null) {
            if (hasCoupon) {
                goodsReqDTO.setIsCoupon(1);
            } else {
                goodsReqDTO.setIsCoupon(0);
            }
        }
        request.setGoodsReqDTO(goodsReqDTO);
        try {
            UnionOpenGoodsQueryResponse response = client.execute(request);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/jd/sku/query", method = RequestMethod.POST)
    public UnionOpenGoodsBigfieldQueryResponse jdGoodsQueryBySkuId(Long[] skuIds) {
        try {
            JdClient client = new DefaultJdClient(JDConfig.JD_URL, "", JDConfig.JD_APPKEY, JDConfig.JD_APPSECRET);
            UnionOpenGoodsBigfieldQueryRequest request = new UnionOpenGoodsBigfieldQueryRequest();
            BigFieldGoodsReq goodsReq = new BigFieldGoodsReq();
            goodsReq.setSkuIds(skuIds);
            request.setGoodsReq(goodsReq);
            UnionOpenGoodsBigfieldQueryResponse response = client.execute(request);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/jd/sku/promotion", method = RequestMethod.POST)
    public UnionOpenGoodsPromotiongoodsinfoQueryResponse jdGoodspromotion(String skuId) {
        try {
            JdClient client=new DefaultJdClient(JDConfig.JD_URL, "", JDConfig.JD_APPKEY, JDConfig.JD_APPSECRET);
            UnionOpenGoodsPromotiongoodsinfoQueryRequest request=new UnionOpenGoodsPromotiongoodsinfoQueryRequest();
            request.setSkuIds(skuId);
            UnionOpenGoodsPromotiongoodsinfoQueryResponse response=client.execute(request);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
